/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author aptech
 */
public class XPathExam {
    public static void main(String[] args) {
        try {
            Document doc = null;
            try {
                doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("student.xml"));
            } catch (SAXException ex) {
                Logger.getLogger(XPathExam.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(XPathExam.class.getName()).log(Level.SEVERE, null, ex);
            }
            XPathFactory xFac = XPathFactory.newInstance();
            XPath xPath = xFac.newXPath();
            NodeList list = null;
            try {
                list = (NodeList) xPath.evaluate("//studentlist/student/name",doc, XPathConstants.NODESET);
            } catch (XPathExpressionException ex) {
                Logger.getLogger(XPathExam.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int i = 0; i < list.getLength(); i++) {
                String s = list.item(i).getTextContent();
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XPathExam.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
